package com.zzw.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.common.Constants;
import com.zzw.springboot.common.Result;
import com.zzw.springboot.entity.Dict;
import com.zzw.springboot.entity.Menu;
import com.zzw.springboot.service.IDictService;
import com.zzw.springboot.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zzw2000
 * @Date 2022-05-28 20:32:01
 * @Description Menu前端控制器
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Resource
    private IDictService dictService;

    /**
     * 保存
     *
     * @param menu
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        return Result.success(menuService.saveOrUpdate(menu));
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(menuService.removeById(id));
    }

    /**
     * 根据id批量删除menu
     *
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        return Result.success(menuService.removeBatchByIds(ids));
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        return Result.success(menuService.findMenus(name));
    }

    /**
     * 查询单条记录
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    @GetMapping("/ids")
    public Result findByIds(){
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示的条数
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        IPage<Menu> menusPage = menuService.page(new Page<>(pageNum, pageSize), Wrappers.<Menu>lambdaQuery()
                .like(StringUtils.isNotEmpty(name), Menu::getName, name)
                .orderByAsc(Menu::getSortNum));
        return Result.success(menusPage);
    }

    @GetMapping("/icon")
    public Result getIcons() {
        List<Dict> dicts = dictService.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getType, Constants.DICT_TYPE_ICON));
        log.info("获取icon图标列表{}",dicts);
        return Result.success(dicts);
    }
}

